﻿<MudTreeView Items="@_treeItems" Hover="true" Width="100%" ServerData="LoadServerData">
    <ItemTemplate Context="item">
        <MudTreeViewItem Items="@item.Children" CanExpand="@item.Expandable">
            <BodyContent>
                <MudIcon Icon="@item.Icon" Size="Size.Small"></MudIcon>
                <MudText>@item.Text</MudText>
                <MudText Typo="Typo.caption">@(((TreeItemData)item).Number)</MudText>
            </BodyContent>
        </MudTreeViewItem>
    </ItemTemplate>
</MudTreeView>

@code {
    private readonly List<TreeItemData<string>> _treeItems = [];

    protected override void OnInitialized()
    {
        _treeItems.Add(new TreeItemData("All Mail", Icons.Material.Filled.Email));
        _treeItems.Add(new TreeItemData("Trash", Icons.Material.Filled.Delete));
        _treeItems.Add(new TreeItemData("Categories", Icons.Material.Filled.Label)
        {
            Children =
            [
                new TreeItemData("Social", Icons.Material.Filled.Group, 90),
                new TreeItemData("Updates", Icons.Material.Filled.Info, 2294),
                new TreeItemData("Forums", Icons.Material.Filled.QuestionAnswer, 3566),
                new TreeItemData("Promotions", Icons.Material.Filled.LocalOffer, 733)
            ]
        });
        _treeItems.Add(new TreeItemData("History", Icons.Material.Filled.Label, null, false));

        base.OnInitialized();
    }

    public async Task<IReadOnlyCollection<TreeItemData<string>>?> LoadServerData(string parentValue)
    {
        if (parentValue is null)
        {
            throw new Exception("Parent value is null!");
        }

        await Task.Delay(500);
        return _treeItems.FirstOrDefault(x => x.Value == parentValue)?.Children;
    }

    public sealed class TreeItemData : TreeItemData<string>
    {
        public TreeItemData(string text, string icon, int? number = null, bool canExpand = true) : base(text)
        {
            Text = text;
            Icon = icon;
            Number = number;
            Expandable = canExpand;
        }

        public int? Number { get; set; }
    }
}